package com.example.day45.animator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.day45.R;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;


@EActivity(R.layout.activity_animator)
public class AnimatorActivity extends AppCompatActivity {
    @ViewById
    ImageView img;
    @ViewById
    Button btn;

    /**
     * 缩放 scaleX，scaleY
     * 旋转 rotationX， rotationY
     * 平移 translationX，translationY, X，Y
     * 透明度 alpha
     */
    private void startAnimator() {
//        TranslateAnimation t = new TranslateAnimation(0, 200, 0, 200);
//        t.setDuration(1000);
//        t.setFillAfter(true);
//        img.startAnimation(t);
        ObjectAnimator.ofFloat(img, "translationX", 0, 300, 200, 100, 0, 500)
                .setDuration(2000).start();
        ObjectAnimator.ofFloat(img, "translationY", 0, 300, 200, 100, 0, 500)
                .setDuration(2000).start();

        ObjectAnimator.ofFloat(img, "rotationX", 0, 360)
                .setDuration(2000).start();
        ObjectAnimator.ofFloat(img, "rotationY", 0, 360)
                .setDuration(2000).start();


        ObjectAnimator.ofFloat(img, "scaleX", 1.0f, 0.5f, 1.0f)
                .setDuration(2000).start();

        ObjectAnimator.ofFloat(img, "scaleY", 1.0f, 0.5f, 1.0f)
                .setDuration(2000).start();


    }


    private void startPropertyValueHolder() {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationX", 0, 300);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("translationY", 0, 300);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("rotationX", 0, 360);
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofFloat("rotationX", 0, 360);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(img, holder1, holder2, holder3, holder4);
        animator.setDuration(1000);
        animator.start();
    }

    private void startAnimatorSet() {
        ObjectAnimator  translationX = ObjectAnimator.ofFloat(img, "translationX", 0, 300, 200, 100, 0, 500);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", 0, 300, 200, 100, 0, 500);
        ObjectAnimator rotationX = ObjectAnimator.ofFloat(img, "rotationX", 0, 360);
        ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", 0, 360);
        /**
         * 执行多个动画
         */
        AnimatorSet set = new AnimatorSet();
        set.play(translationX).with(rotationX).after(translationY);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

//        set.playSequentially(translationX, translationY, rotationX, rotationY);
        set.setDuration(1000);
        set.start();
//        一直执行动画
//        set.playTogether();
    }


    @Click({R.id.btn, R.id.img})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
//                startAnimator();
                startAnimatorSet();
                break;
            case R.id.img:
                break;
        }
    }

    private void startValueAnimator(){
//        ValueAnimator.ofArgb();
    }


}
